package com.tiny.mirror.service.product.application.command;

import com.api.tiny.mirror.service.product.request.CreatePlatformRequest;
import com.api.tiny.mirror.service.product.request.UpdatePlatformAbilityRequest;
import com.api.tiny.mirror.service.product.request.UpdatePlatformRequest;
import com.tiny.mirror.service.product.application.convert.PlatformConvert;
import com.tiny.mirror.service.product.domain.platform.Platform;
import com.tiny.mirror.service.product.domain.platform.repository.PlatformRepository;
import com.tiny.mirror.service.product.domain.platform.service.PlatformDomainService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: liujsh
 * @Description:
 * @Date: Create in 2023/9/27
 */
@Service
public class PlatformCmdService {

  @Resource
  private PlatformDomainService platformDomainService;
  @Resource
  private PlatformRepository platformRepository;

  @Transactional
  public Long create(CreatePlatformRequest request) {
    Platform app = PlatformConvert.convertToPlatform(request);
    return platformDomainService.create(app);
  }

  @Transactional
  public void update(UpdatePlatformRequest request) {
    Platform app = PlatformConvert.convertToPlatform(request);
    platformDomainService.update(request.getId(), app);
  }

  @Transactional
  public Integer delete(List<Long> ids) {
    int result = 0;
    for (Long id : ids) {
      platformDomainService.delete(id);
      result++;
    }
    return result;
  }

  @Transactional
  public void updateAuthAbility(UpdatePlatformAbilityRequest request) {
    platformRepository.savePlatformAbility(request.getPlatformId(), request.getAbilityIds());
  }
}
